//
//  DeviceRealTimeDataCell.m
//  efairy
//
//  Created by 李锋 on 2019/1/18.
//  Copyright © 2019年 East. All rights reserved.
//

#import "DeviceRealTimeDataCell.h"
#import "DeviceListCell.h"
#import "EFAMsgControlCVC.h"
#import "DeviceDetailInfo.h"
#import "DeviceChartVC.h"

@implementation DeviceRealTimeDataCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self initView];
    }
    return self;
}

-(void)initView{
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = ColorFromRGB(0xEDEEF0);
    
    _deviceChart = [[UIView alloc]init];//设备图表
    UILabel *chartTitle = [[UILabel alloc]init];
    chartTitle.font = [UIFont boldSystemFontOfSize:16.f];
    chartTitle.textColor = ColorFromRGB(0x333333);
    chartTitle.text = @"设备图表";
    UIImageView *chartImage= [[UIImageView alloc]init];
    chartImage.image = [UIImage imageNamed:@"icon_right_arrow"];
    [_deviceChart sd_addSubviews:@[chartImage,chartTitle]];
    
    UITapGestureRecognizer *tapChart=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickChartView:)];
    //    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    //    [ges addTarget:self action:@selector(haha)];
    [_deviceChart addGestureRecognizer:tapChart];
   
    
    chartTitle.sd_layout
    .widthIs(100)
    .heightIs(16)
    .topSpaceToView(_deviceChart, 15)
    .leftSpaceToView(_deviceChart, 12);
    
    chartImage.sd_layout
    .widthIs(8)
    .heightIs(13)
    .topSpaceToView(_deviceChart, 16)
    .rightSpaceToView(_deviceChart, 12);
    
    UIView *line2 = [[UIView alloc]init];
    line2.backgroundColor = ColorFromRGB(0xEDEEF0);
    
    _deviceMessage = [[UIView alloc]init];//设备消息
    UILabel *messagetitle = [[UILabel alloc]init];
    messagetitle.font = [UIFont boldSystemFontOfSize:16.f];
    messagetitle.textColor = ColorFromRGB(0x333333);
     messagetitle.text = @"设备消息";
    UIImageView *messageImage = [[UIImageView alloc]init];
    messageImage.image = [UIImage imageNamed:@"icon_right_arrow"];
    [_deviceMessage sd_addSubviews:@[messageImage,messagetitle]];
    
    UITapGestureRecognizer *tapMessage=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickMessageView:)];
    //    UITapGestureRecognizer * ges = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(click:)];
    //    [ges addTarget:self action:@selector(haha)];
    [_deviceMessage addGestureRecognizer:tapMessage];
    
    messagetitle.sd_layout
    .widthIs(100)
    .heightIs(16)
    .topSpaceToView(_deviceMessage, 15)
    .leftSpaceToView(_deviceMessage, 12);
    
    messageImage.sd_layout
    .widthIs(8)
    .heightIs(13)
    .topSpaceToView(_deviceMessage, 16)
    .rightSpaceToView(_deviceMessage, 12);
    
    UIView *line3 = [[UIView alloc]init];
    line3.backgroundColor = ColorFromRGB(0xEDEEF0);
    
    _realTimeTitle = [[UIView alloc]init];//实时数据title
    UILabel *title = [[UILabel alloc]init];
    title.font = [UIFont boldSystemFontOfSize:16.f];
    title.textColor = ColorFromRGB(0x333333);
    title.text = @"实时数据";
    UIImageView *titleImage = [[UIImageView alloc]init];
    titleImage.image = [UIImage imageNamed:@"icon_label"];
    [_realTimeTitle sd_addSubviews:@[titleImage,title]];
    
    
    title.sd_layout
    .widthIs(100)
    .heightIs(16)
    .topSpaceToView(_realTimeTitle, 15)
    .leftSpaceToView(titleImage, 6);
    
    titleImage.sd_layout
    .widthIs(6)
    .heightIs(22)
    .topSpaceToView(_realTimeTitle, 13)
    .leftSpaceToView(_realTimeTitle, 0);
    

    _onlineTitle = [[UILabel alloc]init];//是否在线title;
    _onlineTitle.font = [UIFont systemFontOfSize:13.f];
    _onlineTitle.textColor = ColorFromRGB(0x666666);
    _onlineTitle.text = @"无线通讯状态";
    
    
    _isOnline = [[UILabel alloc]init];//是否在线;
    _isOnline.font = [UIFont systemFontOfSize:13.f];
    
    
    _imgIsOnline = [[UIImageView alloc]init];//是否在线
//    _imgIsOnline.image = [UIImage imageNamed:@"icon_green_point"];
    
    
    
    _signalLevel = [[UIImageView alloc]init];//信号强度
//    _signalLevel.image = [UIImage imageNamed:@"icon_signal_no"]
    
    _signalLevelText = [[UILabel alloc]init];//信号
    _signalLevelText.font = [UIFont systemFontOfSize:13.f];
    _signalLevelText.textColor = ColorFromRGB(0x999999);
    _signalLevelText.text = @"信号";
    
    UIView *line4 = [[UIView alloc]init];
    line4.backgroundColor = ColorFromRGB(0xE5E5E5);
    
    _alarmTypeTitle = [[UILabel alloc]init];//报警状态
    _alarmTypeTitle.font = [UIFont systemFontOfSize:13.f];
    _alarmTypeTitle.textColor = ColorFromRGB(0x666666);
    _alarmTypeTitle.text = @"报警状态";
    
    _alarmType = [[UILabel alloc]init];//报警状态
    _alarmType.font = [UIFont systemFontOfSize:13.f];
    _alarmType.textAlignment = NSTextAlignmentRight;
    
    UIView *line5 = [[UIView alloc]init];
    line5.backgroundColor = ColorFromRGB(0xE5E5E5);
    
    _dataTimeTitle = [[UILabel alloc]init];//数据更新时间
    _dataTimeTitle.font = [UIFont systemFontOfSize:13.f];
    _dataTimeTitle.textColor = ColorFromRGB(0x666666);
    _dataTimeTitle.text = @"数据更新时间";
    
    _dataTime = [[UILabel alloc]init];//数据更新时间
    _dataTime.font = [UIFont systemFontOfSize:13.f];
    _dataTime.textColor = ColorFromRGB(0x999999);
    _dataTime.textAlignment = NSTextAlignmentRight;
    
    UIView *line6 = [[UIView alloc]init];
    line6.backgroundColor = ColorFromRGB(0xE5E5E5);

    _heartbeatTitle = [[UILabel alloc]init];//心跳间隔
    _heartbeatTitle.font = [UIFont systemFontOfSize:13.f];
    _heartbeatTitle.textColor = ColorFromRGB(0x666666);
    _heartbeatTitle.text = @"心跳间隔";
    
    _heartbeatInterval = [[UILabel alloc]init];//心跳间隔
    _heartbeatInterval.font = [UIFont systemFontOfSize:13.f];
    _heartbeatInterval.textColor = ColorFromRGB(0x999999);
    _heartbeatInterval.textAlignment = NSTextAlignmentRight;
    
    UIView *line7 = [[UIView alloc]init];
    line7.backgroundColor = ColorFromRGB(0xE5E5E5);
    
    _audioOnTitle = [[UILabel alloc]init];//音响模式
    _audioOnTitle.font = [UIFont systemFontOfSize:13.f];
    _audioOnTitle.textColor = ColorFromRGB(0x666666);
    _audioOnTitle.text = @"音响模式";
    
    _audioOn = [[UILabel alloc]init];//音响模式
    _audioOn.font = [UIFont systemFontOfSize:13.f];
    _audioOn.textColor = ColorFromRGB(0x999999);
    _audioOn.textAlignment = NSTextAlignmentRight;
    
  
    
    [self.contentView sd_addSubviews:@[_deviceChart,_deviceMessage,_realTimeTitle,_onlineTitle,
                                       _isOnline,_imgIsOnline,_signalLevel,_signalLevelText,
                                       _alarmTypeTitle,_alarmType,_dataTimeTitle,_dataTime,
                                       _heartbeatTitle, _heartbeatInterval,_audioOnTitle,_audioOn,
                                       line,line2,line3,line4,line5,line6,line7]];
    
    line.sd_layout.heightIs(8)
    .widthIs(SCREEN_WIDTH)
    .topSpaceToView(self.contentView, 0);
    
    _deviceChart.sd_layout.heightIs(45)
    .widthIs(SCREEN_WIDTH)
    .topSpaceToView(line, 0);
    
    line2.sd_layout.heightIs(8)
    .widthIs(SCREEN_WIDTH)
    .topSpaceToView(_deviceChart, 0);
    
    _deviceMessage.sd_layout.heightIs(45)
    .widthIs(SCREEN_WIDTH)
    .topSpaceToView(line2, 0);
    
    line3.sd_layout.heightIs(8)
    .widthIs(SCREEN_WIDTH)
    .topSpaceToView(_deviceMessage, 0);
    
    _realTimeTitle.sd_layout.heightIs(45)
    .widthIs(SCREEN_WIDTH)
    .topSpaceToView(line3, 0);
    
    _onlineTitle.sd_layout.heightIs(13)
    .widthIs(80)
    .topSpaceToView(_realTimeTitle, 12)
    .leftSpaceToView(self.contentView, 12);
    
    _imgIsOnline.sd_layout.widthIs(8)
    .heightIs(8)
    .topSpaceToView(_realTimeTitle, 14)
    .leftSpaceToView(_onlineTitle, 156);
    
    _isOnline.sd_layout.heightIs(13)
    .widthIs(27)
    .topSpaceToView(_realTimeTitle, 12)
    .leftSpaceToView(_imgIsOnline, 6);
    
    _signalLevel.sd_layout.heightIs(11)
    .widthIs(15)
    .topSpaceToView(_realTimeTitle, 13)
    .leftSpaceToView(_isOnline, 29);
    
    _signalLevelText.sd_layout.heightIs(13)
    .widthIs(27)
    .topSpaceToView(_realTimeTitle, 12)
    .leftSpaceToView(_signalLevel, 5);
    
    line4.sd_layout.heightIs(0.5)
    .widthIs(SCREEN_WIDTH)
    .leftSpaceToView(self.contentView, 11)
    .topSpaceToView(_onlineTitle, 12);
    
    _alarmTypeTitle.sd_layout.heightIs(13)
    .widthIs(80)
    .topSpaceToView(line4, 12)
    .leftSpaceToView(self.contentView, 12);
    
    _alarmType.sd_layout.heightIs(13)
    .topEqualToView(_alarmTypeTitle)
    .leftSpaceToView(_alarmTypeTitle, 6)
    .rightSpaceToView(self.contentView, 12);
    
    line5.sd_layout.heightIs(0.5)
    .widthIs(SCREEN_WIDTH)
    .leftSpaceToView(self.contentView, 11)
    .topSpaceToView(_alarmTypeTitle, 12);
    
    _dataTimeTitle.sd_layout.heightIs(13)
    .widthIs(80)
    .topSpaceToView(line5, 12)
    .leftSpaceToView(self.contentView, 12);
    
    _dataTime.sd_layout.heightIs(13)
    .topEqualToView(_dataTimeTitle)
    .leftSpaceToView(_dataTimeTitle, 6)
    .rightSpaceToView(self.contentView, 12);
    
    line6.sd_layout.heightIs(0.5)
    .widthIs(SCREEN_WIDTH)
    .leftSpaceToView(self.contentView, 11)
    .topSpaceToView(_dataTimeTitle, 12);
    
    
    _heartbeatTitle.sd_layout.heightIs(13)
    .widthIs(80)
    .topSpaceToView(line6, 12)
    .leftSpaceToView(self.contentView, 12);
    
    
    _heartbeatInterval.sd_layout.heightIs(13)
    .topEqualToView(_heartbeatTitle)
    .leftSpaceToView(_heartbeatTitle, 6)
    .rightSpaceToView(self.contentView, 12);
    
    
    line7.sd_layout.heightIs(0.5)
    .widthIs(SCREEN_WIDTH)
    .leftSpaceToView(self.contentView, 11)
    .topSpaceToView(_heartbeatTitle, 12);
    
    
    _audioOnTitle.sd_layout.heightIs(13)
    .widthIs(80)
    .topSpaceToView(line7, 12)
    .leftSpaceToView(self.contentView, 12);
    
    _audioOn.sd_layout.heightIs(13)
    .topEqualToView(_audioOnTitle)
    .leftSpaceToView(_audioOnTitle, 6)
    .rightSpaceToView(self.contentView, 12);
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self setupAutoHeightWithBottomView:_audioOn bottomMargin:10];
}

-(void)setModeData:(DeviceRealTimeInfo *)info{

    if(info!= nil){
       //是否在线0-否 1-是
        if(info.efairydevice_is_online == 0){
            _imgIsOnline.image =  [UIImage imageNamed:@"icon_gray_point"];
            _isOnline.text  = @"离线";
            _isOnline.textColor = ColorFromRGB(0xBBBBBB);
             _signalLevel.image = [UIImage imageNamed:@"icon_signal_no"];
        }else{
            _imgIsOnline.image =  [UIImage imageNamed:@"icon_green_point"];
            _isOnline.textColor = ColorFromRGB(0x3FB059);
            _isOnline.text  = @"在线";
            if(info.efairydevice_csq_level == 0){
                _signalLevel.image = [UIImage imageNamed:@"icon_signal_weak"];
            }else if(info.efairydevice_csq_level == 1){
                _signalLevel.image = [UIImage imageNamed:@"icon_signal_mediu"];
            }else{
                _signalLevel.image = [UIImage imageNamed:@"icon_signal_strong"];
            }
        }
       
        _alarmType.text = [DeviceListCell getDeviceState:(int)info.efairydevice_alarm_id];
        _alarmType.textColor = ColorFromRGB([DeviceListCell getDeviceStateColor:(int)info.efairydevice_alarm_id]);
        _dataTime.text = info.rt_data_time;
        _heartbeatInterval.text = [NSString stringWithFormat:@"%ldS",info.heartbeat_interval];
        if(info.audio_on == 0){
            _audioOn.text = @"静音";
        }else{
            _audioOn.text = @"音响";
        }
      
    }
    
}

//进入设备图标页
-(void)onClickChartView:(id)tap{
    UIViewController *uvc = [self parentController];
    if(uvc != nil){
        DeviceChartVC *vc = [[DeviceChartVC alloc]init];
        vc.deviceId = _deviceId;
        [uvc showViewController:vc sender:nil];
    }
    
}

//进入消息页
-(void)onClickMessageView:(id)tap{
    UIViewController *uvc = [self parentController];
    if(uvc != nil){
        UIStoryboard *mainSb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        EFAMsgControlCVC *vc = [mainSb instantiateViewControllerWithIdentifier:@"EFAMsgControlCVC"];
        vc.device_id = _deviceId;
        vc.device_name = _deviceName;
        [uvc showViewController:vc sender:nil];
    }
    
}

//获取cell所在的UIViewController
- (UIViewController *)parentController
{
    for(UIView * next =[self superview]; next; next = next.superview){
        UIResponder *nextResponder= [next nextResponder];
        if([nextResponder isKindOfClass:[DeviceDetailInfo class]]){
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


@end
